//
//  ViewController.m
//  环形进度
//
//  Created by zhouLiHuan on 2017/2/14.
//  Copyright © 2017年 zhouLiHuan. All rights reserved.
//

#import "ViewController.h"
#import "LHDownloadCircleView.h"

@interface ViewController ()

@property (nonatomic,strong)LHDownloadCircleView *downCircleView;

@property(nonatomic,strong)UITextField *numTextFied;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.downCircleView = [[LHDownloadCircleView alloc] initWithFrame:CGRectMake((self.view.bounds.size.width - 100)*0.5, 100, 100, 100)];
    [self.view addSubview:self.downCircleView];
    
    self.numTextFied = [[UITextField alloc] initWithFrame:CGRectMake(20, 400, self.view.frame.size.width - 40, 44)];
    self.numTextFied.placeholder = @"请输入0~100的比例";
    [self.view addSubview:self.numTextFied];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setTitle:@"开始下载" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.frame = CGRectMake(150,CGRectGetMaxY(self.numTextFied.frame) + 10, self.view.frame.size.width- 300, 40);
    [button addTarget:self action:@selector(download) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (void)download{
    
    if (self.numTextFied.text  == nil) {
        NSLog(@"比例不能为空");
        return;
        
    }
    
    if (![self isNumber:self.numTextFied.text] ) {
        NSLog(@"必须为数字");
        return;
    }
    //点击下载按钮
    self.downCircleView.rate = [self.numTextFied.text integerValue];
   
    [self.downCircleView startAnimation];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/* 是否是数字 整数 小数 */
- (BOOL)isNumber:(NSString *)comStr{
   
        
        NSString *pattern = @"^[0-9]+([.]{0,1}[0-9]+){0,1}$";
        
        NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
        //反馈一个字符串
        NSArray *allDate = [regularExpression matchesInString:comStr options:NSMatchingReportCompletion range:NSMakeRange(0, comStr.length)];
        
        if (allDate.count == 0) {
            return NO;
        }else{
            return YES;
        }
    }

@end
